package com.skydroid.fpvplayerdemo;

import android.os.Bundle;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.skydroid.fpvplayer.FPVReaderWidget;
import com.skydroid.rcsdk.PipelineManager;
import com.skydroid.rcsdk.comm.CommListener;
import com.skydroid.rcsdk.common.error.SkyException;
import com.skydroid.rcsdk.common.pipeline.Pipeline;

/**
 * @author 咔一下
 * @date 2024/1/23 16:09
 * @email 1501020210@qq.com
 * @describe 串口图传示例
 */
public class SerialPortFPVActivity extends AppCompatActivity {

    //串口图传播放控件
    FPVReaderWidget fpvReaderWidget = null;

    //串口图传数据通信
    Pipeline pipeline = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_serialport_fpv);

        fpvReaderWidget = findViewById(R.id.fpvReaderWidget);

        //三体相机与双轴云台需要使用OpenH264进行软解
        fpvReaderWidget.setVideoDecoderType(FPVReaderWidget.VIDEO_DECODER_TYPE_OPENH264);

        //配置串口信息
        pipeline = PipelineManager.INSTANCE.createSerialPipeline("/dev/ttyHS0",4000000);
        pipeline.setOnCommListener(new CommListener() {

            @Override
            public void onConnectSuccess() {
                //连接串口成功
            }

            @Override
            public void onConnectFail(SkyException e) {
                //连接串口失败
            }

            @Override
            public void onDisconnect() {
                //串口断开连接
            }

            @Override
            public void onReadData(byte[] bytes) {
                //收到串口数据,发送到串口图传播放控件进行解码显示
                fpvReaderWidget.sendFrame(bytes);
            }
        });

        //连接串口
        PipelineManager.INSTANCE.connectPipeline(pipeline);

        //设置参数开始播放
        fpvReaderWidget.start(FPVReaderWidget.VIDEO_TYPE_H264,640,360,15);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //停止播放
        fpvReaderWidget.stop();

        //断开串口连接
        PipelineManager.INSTANCE.disconnectPipeline(pipeline);
    }

}
